package com.atguigu.gmall.list.controller;

import com.atguigu.gmall.common.result.Result;
import com.atguigu.gmall.list.service.SearchService;
import com.atguigu.gmall.model.list.Goods;
import com.atguigu.gmall.model.list.SearchParam;
import com.atguigu.gmall.model.list.SearchResponseVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.elasticsearch.core.ElasticsearchRestTemplate;
import org.springframework.web.bind.annotation.*;

import java.io.IOException;

/**
 * @author atguigu-mqx
 */
@RestController
@RequestMapping("api/list")
public class ListApiController {

    @Autowired
    private ElasticsearchRestTemplate restTemplate;

    @Autowired
    private SearchService searchService;

    @GetMapping("inner/createIndex")
    public Result createIndex(){
        //  调用方法自动创建mapping 6.8.1  现在 7.8.0 它有自己的创建方式！
        restTemplate.createIndex(Goods.class);
        restTemplate.putMapping(Goods.class);
        return Result.ok();
    }

    //  商品的上架，商品下架
    @GetMapping("inner/upperGoods/{skuId}")
    public Result upperGoods(@PathVariable Long skuId){
        searchService.upperGoods(skuId);
        return Result.ok();
    }

    @GetMapping("inner/lowerGoods/{skuId}")
    public Result lowerGoods(@PathVariable Long skuId){
        searchService.lowerGoods(skuId);
        return Result.ok();
    }

    //  发布一个热度排名数据接口
    @GetMapping("inner/incrHotScore/{skuId}")
    public Result incrHotScore(@PathVariable Long skuId){
        searchService.updHotScore(skuId);
        return Result.ok();
    }

    //  商品检索： web-all 能够接收到传递的参数：searchParam 需要一个远程调用 {有可能需要传递多个数据 ，需要使用Json 对象接收}
    @PostMapping
    public Result searchData(@RequestBody SearchParam searchParam){
        SearchResponseVo searchResponseVo = null;
        try {
            searchResponseVo = searchService.search(searchParam);
        } catch (IOException e) {
            e.printStackTrace();
        }
        return Result.ok(searchResponseVo);
    }
}
